//
//  UIView+Chain.m
//  ditelMaintain
//
//  Created by 葛悦彣 on 2023/3/2.
//

#import "UIView+Chain.h"

@implementation UIView (Chain)

- (instancetype _Nonnull (^)(UIColor * _Nonnull))addbackgroundColor{
    return ^(UIColor * color){
        self.backgroundColor = color;
        return self;
    };
}

- (instancetype _Nonnull (^)(CGFloat))addYJ {
    return ^(CGFloat a){
        self.layer.cornerRadius = a;
        self.layer.masksToBounds = YES;
        return self;
    };
}

- (instancetype  _Nonnull (^)(void (^ _Nonnull)(void)))addClickBlock {
    return ^(void(^block)(void)){
        self.clickBlock = block;
        self.userInteractionEnabled = YES;
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickview)];
        tap.delegate = self;
        ///再手势拦截的地方加- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
        [self addGestureRecognizer:tap];
        return self;
    };
}

-(void)clickview{
    if(self.clickBlock){
        self.clickBlock();
    }
}

///类别添加对象
static char *PersonNameKey = "chainClickBlock";
-(void)setClickBlock:(void (^)(void))clickBlock{
    objc_setAssociatedObject(self, PersonNameKey, clickBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
-(void (^)(void))clickBlock{
    return objc_getAssociatedObject(self, PersonNameKey);
}
@end
